<template>
  <div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="handleBlack">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="blacklistList" border>
      <el-table-column label="商品编码" align="center" prop="goodsId" />
      <el-table-column label="串号" align="center" prop="imei" />
      <el-table-column label="规格" align="center" prop="specs" />
      <el-table-column label="创建人名称" align="center" prop="createName" />
      <el-table-column label="创建人名称" align="center" prop="createTime" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['recovery:blacklist:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :visible.sync="blackOpen" title="添加黑名单" width="600px" append-to-body>
      <el-form label-width="80px" v-if="blackForm">
        <el-form-item label="商品编码"> {{blackForm.goodsId}} </el-form-item>
        <el-form-item label="串号"> {{blackForm.imei}} </el-form-item>
        <el-form-item label="规格"> {{blackForm.specs}} </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="blackForm.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="blackSubmit">确 定</el-button>
        <el-button @click="blackCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listBlacklist, delBlacklist, addBlacklist} from "@/api/recovery/blacklist";

export default {
  name: "Blacklist",
  props: {
    goodsInfo: {type: Object},
    goodsSn: {type: String}
  },
  watch: {
    goodsSn(val) {
      this.queryParams.imei = val;
      this.getList();
    } ,
  },
  data() {
    return {
      blackOpen: false,
      blackForm: undefined,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品黑名单表格数据
      blacklistList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsId: null,
        imei: null,
        specs: null,
        createName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    if(this.goodsSn) {
      this.queryParams.imei = this.goodsSn;
    }
    this.getList();
  },
  methods: {
    handleBlack() {
      this.blackForm = {
        goodsId: this.goodsInfo.goodsId,
        imei: this.goodsInfo.goodsSn,
        specs: this.goodsInfo.printing,
        remark: undefined
      }
      this.blackOpen = true;
    },
    blackCancel() {
      this.blackForm = undefined;
      this.blackOpen = false;
    },
    blackSubmit() {
      addBlacklist(this.blackForm).then(res => {
        this.blackOpen = false;
        this.msgSuccess("操作成功!");
        this.getList();
      })
    },
    /** 查询商品黑名单列表 */
    getList() {
      this.loading = true;
      listBlacklist(this.queryParams).then(response => {
        this.blacklistList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除商品黑名单编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return delBlacklist(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
  }
};
</script>
